//
//  RBStockOrderListVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/17.
//

#import "RBStockOrderListVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "RBStockOrderListCell.h"
#import "RBStockOrderDetailVC.h"
#import "RBStockOrderResponse.h" //model
#import "RBStockAddOrderVC.h" //加工订单
#import "RBCashierVC.h" //收银台

@interface RBStockOrderListVC ()
<UICollectionViewDataSource, UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout,
RBStockOrderDetailDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, strong) RBStockOrderListResponse *response;
@end

@implementation RBStockOrderListVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self.listView.mj_header beginRefreshing];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.sectionInset = UIEdgeInsetsMake(Zoom(10), 0, Zoom(10), 0);
        group.minimumLineSpacing = Zoom(10);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
}

- (void)appendData {
    LCWeakSelf;
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.lastObject;
        (NSMutableArray *)group.cellModels;
    });
    for (RBOrderListModel *model in self.response.data) {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBStockOrderListCell.class);
            cellModel.cellData = model;
            cellModel.eventBlock1 = ^(NSInteger type, NSInteger index, RBOrderListModel *data) {
                switch (index) {
                    case 1 : { //查看订单
                        RBStockOrderDetailVC *vc = RBStockOrderDetailVC.new;
                        vc.order_id = data.order_id;
                        vc.delegate = weakSelf;
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    }   break;
                    case 2 : { //加工订单
                        RBAddOrderSubmitModel *pModel = RBAddOrderSubmitModel.new;
                        pModel.parent_id = data.order_id;
                        pModel.goods_id = data.goods_id;
                        pModel.total_num = data.quantity;
                        pModel.order_num = data.order_number;
                        
                        RBStockAddOrderVC *vc = RBStockAddOrderVC.new;
                        vc.submitModel = pModel;
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    }   break;
                    case 3 : { //支付
                        [weakSelf goPayWith:data];
                    }   break;
                }
            };
            cellModel;
        })];
    }
    
    [self.collectionView reloadData];
    
    if(!arrayM.count)
    {
        [self.collectionView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}

#pragma mark - Network
- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
    dicM[@"user_id"] = [RBAccountManager getUserId];
    dicM[@"order_type"] = @"5";
    dicM[@"status"] = @(self.status);
    return dicM;
}
- (NSString *)listRequestPath
{
    return _Order_list;
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBStockOrderListResponse.class;
}
- (UIScrollView *)listView
{
    return self.collectionView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.listView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}
- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}
#pragma mark - Event
//去支付
- (void)goPayWith:(RBOrderListModel *)model {
    RBCashierVC *vc = RBCashierVC.new;
    vc.isFromOrder = YES;
    vc.order_id = model.order_id;
    vc.order_num = model.order_number;
    vc.order_price = model.total_price;
    LCWeakSelf;
    vc.block = ^{
        [weakSelf.listView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark RBStockOrderDetailDelegate
//刷新
- (void)paySuccessRefreshListAction {
    [self.listView.mj_header beginRefreshing];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegateAuto(self.dataSource);

#pragma mark - Setter

#pragma mark - Getter

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
            UICollectionView *view =  [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(0);
            }];
            view;
        });
    }
    return _collectionView;
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

@end
